/*
 * DestinationScreen.java
 *
 */
package com.gpsnav.dao;

import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.InvalidRecordIDException;
import java.io.IOException;
import com.gpsnav.bo.Options;
import com.gpsnav.bo.Waypoint;

import net.rim.device.api.ui.component.Dialog;

public class WaypointsDAO {

    private static String rsName = "GPSProject_waypoints";

    public WaypointsDAO() {
    }


    public Waypoint[] loadWaypoints() {

        byte[] data = null;
        Waypoint [] wpList = new Waypoint[0];

        try {
            RecordStore rs = RecordStore.openRecordStore( rsName, true );
            int    nextID = rs.getNextRecordID();
            wpList = new Waypoint[rs.getNumRecords()];
            int i = 0;
            for( int id = 0; id < nextID; ++id ){
                 try {
                      int size = rs.getRecordSize( id );

                      if( data == null || data.length < size ){
                          data = new byte[ size ];
                      }

                      rs.getRecord( id, data, 0 );

                      Waypoint wp = new Waypoint();
                      wp.fromByteArray(data);
                      wp.setKey(id);
                      wpList[i++] = wp;
                } catch( InvalidRecordIDException e ){
                   // ignore, move to next record
                } catch( IOException e ){
                   // ignore, move to next record
                }
            }
            rs.closeRecordStore();
        } catch( RecordStoreException e ){
           // handleError( rs, id, e ); // call an error routine
        }

        return wpList;
    }

    public void storeWaypoint(Waypoint wp) {

        try {
            RecordStore rs = RecordStore.openRecordStore( rsName, true );
            byte[] data = wp.toByteArray();
System.out.println("wp id = " + wp.getKey());
            if (wp.getKey() < 0) {
                int key = rs.addRecord( data, 0, data.length );
                wp.setKey(key);
            } else {
                rs.setRecord( wp.getKey(), data, 0, data.length );
            }

            rs.closeRecordStore();
        }
        catch( RecordStoreException e ){
            // handle the RMS error here
            System.out.println("Error saving waypoint" + e.getMessage());
            Dialog.inform("Error saving waypoint" + e.getMessage());
        }
        catch( IOException e ){
            // handle the IO error here
            System.out.println("IOException Error saving waypoint" + e.getMessage());
            Dialog.inform("IOExceptionError saving waypoint" + e.getMessage());
        }

    }

    public static int getNextId() {

        int id = 0;
        try {
            RecordStore rs = RecordStore.openRecordStore( rsName, true );
            id = rs.getNextRecordID();
            rs.closeRecordStore();
        }
        catch( RecordStoreException e ){
            // handle the RMS error here
        }
        return id;
    }

    public void deleteWaypoint(Waypoint wp) {

        try {
            RecordStore rs = RecordStore.openRecordStore( rsName, true );

            if (wp.getKey() < 0) {
                //wp = null;
            } else {
                rs.deleteRecord(wp.getKey());
            }

            rs.closeRecordStore();
        }
        catch( RecordStoreException e ){
            // handle the RMS error here
        }

    }

 /*   public void storeWaypoints(ArrayList wpList) {

        try {
            RecordStore rs = RecordStore.openRecordStore( rsName, true );

            for(Enumeration enum1 = wpList.getEnumeration();enum1.hasMoreElements();) {
              Waypoint wp = (Waypoint) enum1.nextElement();

                byte[] data = wp.toByteArray();
                if (wp.getKey() < 0) {
                    rs.addRecord( data, 0, data.length );
                } else {
                    rs.setRecord( wp.getKey(), data, 0, data.length );
                }
            }
            rs.closeRecordStore();
        }
        catch( RecordStoreException e ){
            // handle the RMS error here
        }
        catch( IOException e ){
            // handle the IO error here
        }

    }
*/
}

